import 'package:android_intent_plus/android_intent.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:sph_shipper/AppConfig/Config.dart';
import 'package:sph_shipper/FandCar/SelectSourceOfGoods.dart';
import 'package:url_launcher/url_launcher.dart';

import '../AppConfig/RouteAct.dart';
import '../AppConfig/ShowDialog.dart';
import '../AppConfig/StringEncapsulation.dart';
import '../l10n/messages.dart';
import 'DriverIntroduction.dart';
import 'package:platform/platform.dart';
class VehicleSourceDetails extends StatefulWidget {
  const VehicleSourceDetails({super.key, required this.dataMap});
  final Map dataMap;
  @override
  State<VehicleSourceDetails> createState() => _VehicleSourceDetailsState();
}

class _VehicleSourceDetailsState extends State<VehicleSourceDetails> {


  _makePhoneCall(String phoneNumber) async {
    if (LocalPlatform().isAndroid) {
      var status = await Permission.phone.status;
      if (status.isGranted) {
        // 权限已被授予
        // callPhone();
        final AndroidIntent intent = AndroidIntent(
          action: 'android.intent.action.CALL',
          data: Uri.encodeFull('tel:$phoneNumber'),
        );
        await intent.launch();
      } else if (status.isDenied) {
        // 权限被拒绝，但用户可以再次授权
        showCallPhoneFirstDialog(context);
        status = await Permission.phone.request();
        if (status.isGranted) {
          // 用户同意了权限
          // callPhone();
          final AndroidIntent intent = AndroidIntent(
            action: 'android.intent.action.CALL',
            data: Uri.encodeFull('tel:$phoneNumber'),
          );
          await intent.launch();
        } else {
          showCallPhoneDialog(context);
          // 用户拒绝权限，无法发起电话
          // 可以在此处展示信息提示用户需要权限
        }
      } else if (status.isPermanentlyDenied) {
        // 权限被永久拒绝，需要用户手动开启权限
        showCallPhoneDialog(context);
      }

    }else{
      final url =Uri.parse('tel:$phoneNumber');
      if (await canLaunchUrl(url)) {
        await launchUrl(url);
      } else {
        throw '无法拨打电话：$url';
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    var messages = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
        title: Text("车源详情",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w300),),
      ),
      body: Column(
        children: [
          Expanded(child:   Container(
            margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h),
            decoration: BoxDecoration(
                // color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: ListView(
              children: [
                Container(
                  padding: EdgeInsets.only(left: 8.w,right: 8.w,top: 15.h),
                  color: Colors.white,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text("空车信息",style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                      SizedBox(height:4.h ,),
                      Row(
                        children: [
                          Image.asset("lib/Images/OrderList/去指派熟车icon.png",width: 20.w,height: 20.h,),
                          SizedBox(width: 4.w,),
                          Text("空车时间",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        ],
                      ),


                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [

                          SizedBox(width: 6.w,),
                          Text(widget.dataMap['loadTime'].substring(0,  widget.dataMap['loadTime'].length - 6).toString()+"点 至 "+widget.dataMap['unloadTime'].substring(0, widget.dataMap['unloadTime'].length - 6).toString()+"点",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),

                        ],
                      ),
                      Divider(height: 10.h,color: MyColors.backgreyColor,),
                      Row(
                        children: [
                          // SizedBox(width: 6.w,),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Image.asset("lib/Images/OrderList/装icon.png",
                                    width: 13.w, height: 13.h,),
                                  SizedBox(width: 7.w,),
                                  if(widget.dataMap['loadPlace']['county']!=null)
                                  Text(widget.dataMap['loadPlace']['province'].toString()+" "+widget.dataMap['loadPlace']['city'].toString()+" "+widget.dataMap['loadPlace']['county'],style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                                  if(widget.dataMap['loadPlace']['county']==null)
                                    Text(widget.dataMap['loadPlace']['province'].toString()+" "+widget.dataMap['loadPlace']['city'].toString(),style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                                ],
                              ),
                             
                              Image.asset(
                                "lib/Images/OrderList/垂直虚线.png", width: 13.w,
                                height: 15.h,),
                              Row(
                                children: [
                                  Image.asset("lib/Images/OrderList/卸icon.png",
                                    width: 13.w, height: 13.h,),
                                  SizedBox(width: 7.w,),
                                  if(widget.dataMap['unloadPlace']['county']!=null)
                                    Text(widget.dataMap['unloadPlace']['province'].toString()+" "+widget.dataMap['unloadPlace']['city'].toString()+" "+widget.dataMap['unloadPlace']['county'],style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                                  if(widget.dataMap['unloadPlace']['county']==null)
                                    Text(widget.dataMap['unloadPlace']['province'].toString()+" "+widget.dataMap['unloadPlace']['city'].toString(),style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                                ],
                              ),
                              SizedBox(height: 8.h,),

                            ],
                          ),
                         
                        ],
                      ),

                    ],
                  ),
                ),
                Container(
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(239, 239, 239, 1),
                      borderRadius: BorderRadius.only(bottomLeft:  Radius.circular(10),bottomRight:  Radius.circular(10))),
                  height: 32.h,
                  child: Row(
                    children: [
                      SizedBox(width: 8.w,),
                      Icon(Icons.error_outline,color: MyColors.authTipColor,size: 20,),
                      SizedBox(width: 7.w,),
                     if(widget.dataMap['vehicleLength']==null)
                       Text("--米,--，--吨",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                      if(widget.dataMap['vehicleLength']!=null)
                      Text(widget.dataMap['vehicleLength'].toString()+"米,"+widget.dataMap['vehicleModel'].toString()+","+widget.dataMap['vehicleTonnage'].toString()+ "吨",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                    ],
                  ),
                ),
                GestureDetector(
                  child: Container(
                    padding: EdgeInsets.only(left: 15.w,top: 14.h,right: 15.w,bottom: 10.h),
                    margin: EdgeInsets.only(top: 8.h),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all( Radius.circular(10))),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.all(Radius.circular(10))),
                                  child: ClipRRect(
                                    borderRadius: BorderRadius.all(Radius.circular(10)),
                                    child: widget.dataMap['avatarPath']!=""&&widget.dataMap['avatarPath']!=null?Image.network(
                                      MyServiceUrl.imgHeader + widget.dataMap['avatarPath'],
                                      width: 36.w,
                                      height: 36.h,
                                      fit: BoxFit.cover,
                                    ):Image.asset("lib/Images/login/默认头像.jpeg",width: 36.w, height: 36.h, fit: BoxFit.cover,),
                                  ),
                                ),
                                SizedBox(width: 8.w,),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(widget.dataMap['realName'],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                                    Text(maskPhoneNumber(widget.dataMap['phoneNum']),style: TextStyle(color: Colors.black,fontSize: 14.sp,),),
                                  ],
                                )

                              ],
                            ),
                            // BrnTagCustom.buildBorderTag(
                            //   tagText:  widget.dataMap['trailerDrivingLisenseStatus']!=2?'三证齐全':"五证齐全",
                            //   textColor:MyColors.themeColor,
                            //   borderColor: MyColors.themeColor,
                            //   borderWidth: 1,
                            //   fontSize: 11.sp,
                            // ),
                          ],
                        ),

                        SizedBox(height: 4.h,),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            // Text(widget.dataMap['vehicleNum'],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                               Row(
                                 children: [
                                   Text("好评率:",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                                   Text(widget.dataMap['rating'].toString()+"%",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                                   SizedBox(width: 8.w,),
                                   Text("信用等级:",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                                 ],
                               ),
                                  Row(
                                    children: List.generate( (divideAndRound(widget.dataMap['rating'].toString(), 20)), (starIndex) {
                                      return Icon(Icons.star, color: MyColors.themeColor, size: 20.0);
                                    }),
                                  ),
                              ],
                            )
                          ],
                        ),
                        SizedBox(height: 4.h,),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            if(widget.dataMap['vehicleLength']!=null)
                            Text(widget.dataMap['vehicleLength'].toString()+"米丨"+widget.dataMap['vehicleModel'].toString()+"丨"+widget.dataMap['vehicleTonnage'].toString()+ "吨",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),

                          ],
                        )
                      ],
                    ),
                  ),
                  onTap: (){
                    Navigator.push( context, FadeRoute(page:DriverIntroduction(DrvId: widget.dataMap['driverId'],)));

                  },
                ),

              ],

            ),
          ),
          ),

          Container(
            color: Colors.white,
            height: 85.h,
            padding: EdgeInsets.only(top: 10.h,bottom: 44.h),
            child:
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                BrnSmallOutlineButton(
                  title: '电话联系',
                  radius: 30,
                  fontSize: 16.sp,
                  width: 130.w,
                  textColor: MyColors.themeColor,
                  lineColor: MyColors.themeColor,
                  onTap: () {
                    _makePhoneCall(widget.dataMap['phoneNum']);
                  },
                ),
                SizedBox(width: 20.w,),
                BrnSmallMainButton(
                  title: '立即派单',
                  textColor: Colors.white,
                  bgColor: MyColors.themeColor,
                  radius: 30,
                  width: 130.w,
                  fontSize: 16.sp,
                  onTap: () {
                    Navigator.push( context, FadeRoute(page:SelectSourceOfGoods(carMap: widget.dataMap,)));
                  },
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
